package com.huntero.hweather;

import java.util.List;

import com.huntero.hweather.adapter.CityCodeAdapter;
import com.huntero.hweather.adapter.ProvinceAdapter;
import com.huntero.hweather.bean.CityCode;
import com.huntero.hweather.widget.HSpinner;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnItemSelected;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;

public class ConfigPreference extends Activity {

	private int mWidgetId=AppWidgetManager.INVALID_APPWIDGET_ID;
	public static final int REQUEST_CODE=0x02;
	
	@ViewInject(R.id.spn_province)
	private HSpinner spnProvince;
	private CityCodeAdapter mAdapterProvince;
	private List<CityCode> mDataProvince;
	
	@ViewInject(R.id.spn_county)
	private HSpinner spnCounty;
	private CityCodeAdapter mAdapterCounty;
	private List<CityCode> mDataCounty;
	
	@ViewInject(R.id.spn_city)
	private HSpinner spnCity;
	private CityCodeAdapter mAdapterCity;
	private List<CityCode> mDataCity;
	
	@ViewInject(R.id.btn_ok)
	private Button btnOk;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preference_config);
		ViewUtils.inject(this);
		
		this.mWidgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
		if(AppWidgetManager.INVALID_APPWIDGET_ID == mWidgetId){
			setResult(RESULT_CANCELED);
			finish();
		}
		
		loadData(this.mAdapterProvince,this.mDataProvince,this.spnProvince,Selector.from(CityCode.class).where("level","=","1"));
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == ConfigPreference.REQUEST_CODE && this.mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID){
			resultCode = RESULT_OK;
		}else{
			resultCode = RESULT_CANCELED;
		}
		
		Intent intent = new Intent();
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, this.mWidgetId);
		setResult(resultCode,intent);
		finish();
	}
	
	private void loadData(BaseAdapter adapter,List<CityCode> data,HSpinner spinner,Selector selector) {
		try {
			data = DbUtils.create(this, Constant.DB_NAME).findAll(selector);
			if(adapter == null){
				adapter = new CityCodeAdapter(this, data);
				spinner.setAdapter(adapter);
			}else{
				adapter.notifyDataSetChanged();
			}
		} catch (DbException e) {
			e.printStackTrace();
		}
	}
	
	@OnItemSelected(R.id.spn_province)
	public void provinceSelectedOfProvince(AdapterView<?> parent, View view,
			int position, long id){
		String key = null;
			//初始化二级菜单
			this.spnCounty.setVisibility(View.VISIBLE);
			key = ((CityCode)this.spnProvince.getSelectedItem()).key;
			loadData(this.mAdapterCounty,this.mDataCounty,this.spnCounty,Selector.from(CityCode.class).where("level","=","2").where("parentKey","=",key));
		Log.i("Huntero", "on itemSelected > " + key);
	}
	@OnItemSelected(R.id.spn_county)
	public void countySelectedOfProvince(AdapterView<?> parent, View view,
			int position, long id){
		String key = null;
			//初始化三级菜单
			this.spnCity.setVisibility(View.VISIBLE);
			key = ((CityCode)this.spnCounty.getSelectedItem()).key;
			loadData(this.mAdapterCity,this.mDataCity,this.spnCity,Selector.from(CityCode.class).where("level","=","3").where("parentKey","=",key));
		Log.i("Huntero", "on itemSelected > " + key);
	}
	@OnItemSelected(R.id.spn_city)
	public void citySelectedOfProvince(AdapterView<?> parent, View view,
			int position, long id){
//		String key = null;
//		switch(view.getId()){
//		case R.id.spn_province:
//			//初始化二级菜单
//			this.spnCounty.setVisibility(View.VISIBLE);
//			key = ((CityCode)this.spnProvince.getSelectedItem()).key;
//			loadData(this.mAdapterCounty,this.mDataCounty,this.spnCounty,new String[]{"parentKey","=",key});
//			break;
//		case R.id.spn_county:
//			//初始化三级菜单
//			this.spnCity.setVisibility(View.VISIBLE);
//			key = ((CityCode)this.spnCounty.getSelectedItem()).key;
//			loadData(this.mAdapterCity,this.mDataCity,this.spnCity,new String[]{"parentKey","=",key});
//			break;
//		case R.id.spn_city:
//			break;
//		}
//		Log.i("Huntero", "on itemSelected > " + key);
	}
	
}
